/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.uefs.cedoc.db.parteII;

/**
 *
 * @author Gregory Porto
 */


import br.uefs.cedoc.db.DBController;
import br.uefs.cedoc.db.exception.DBConnectionException;
import br.uefs.cedoc.entidades.ParteIBean;
import br.uefs.cedoc.entidades.ParteIIBean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Gregory Porto
 */
public class DBParteII {

    public void criarParteII(ParteIIBean parteI) throws SQLException, DBConnectionException {

        String comandoSQL = "INSERT INTO parteII (nome,prenome) VALUES(?,?)";

        Connection conn = DBController.getInstance().getConnection();

        if (conn != null) {

            PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);
            prepStatement.setString(1, parteI.getNome());
            prepStatement.setString(2, parteI.getPrenome());
            prepStatement.execute();
		
            prepStatement.close();
        } else {
            DBConnectionException dbException = new DBConnectionException(" Erro de conexao com banco ");
            dbException.printStackTrace();
            throw dbException;
        }
    }

    public synchronized void editarParteII(ParteIIBean parteII) throws SQLException, DBConnectionException {

        String comandoSQL = "UPDATE parteII SET nome = ? , prenome=? WHERE idparteII = ?";

        Connection conn = DBController.getInstance().getConnection();
        //System.out.println("Editar acervo: "+acervo.getNome());
        if (conn != null) {

            PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);
            prepStatement.setString(1, parteII.getNome());
            prepStatement.setString(2, parteII.getPrenome());
            prepStatement.setInt(3, parteII.getIdParteII());
            prepStatement.execute();
            prepStatement.close();

        } else {
            DBConnectionException dbException = new DBConnectionException(" Erro de conex�o com banco ");
            dbException.printStackTrace();
            throw dbException;
        }


    }

    public ParteIIBean listaParteII(int id) throws SQLException, DBConnectionException {
        String comandoSQL = "SELECT * FROM parteII WHERE idparteII=?"; // incluir
        // limites

        Connection conn = DBController.getInstance().getConnection();
        try {
            if (conn != null) {
                PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);

                prepStatement.setInt(1,id);
                ResultSet rs = prepStatement.executeQuery();



                while (rs.next()) {
                    ParteIIBean parteII = new ParteIIBean();

                    parteII.setIdParteII(rs.getInt("idparteII"));
                    parteII.setNome(rs.getString("nome"));
                    parteII.setPrenome(rs.getString("prenome"));
                    
                    return parteII;

                }
                DBController.getInstance().closeAll(conn, null, null);
               


            } else {
                DBConnectionException dbException = new DBConnectionException(" Erro de conex�o com banco ");
                dbException.printStackTrace();
                throw dbException;
            }
            return null;
        } catch (NullPointerException ex) {
            ex.printStackTrace();
            return null;
        }
    }



    	public  void removerParteII(int id) throws SQLException {

		String comandoSQL = "DELETE FROM parteII WHERE idparteII=?";

		Connection conn = DBController.getInstance().getConnection();
		
		if (conn != null){
			

			PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);
			prepStatement.setInt(1, id);						
			prepStatement.execute();			
			prepStatement.close();
		}	

	}

    public ParteIIBean[] listarParteIs() throws SQLException, DBConnectionException {
        String comandoSQL = "SELECT * FROM parteII "; // incluir
        // limites

        Connection conn = DBController.getInstance().getConnection();
        try {
            if (conn != null) {
                PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);


                ResultSet rs = prepStatement.executeQuery();

                Collection parteIIs = new ArrayList();

                while (rs.next()) {
                    ParteIIBean parteII_temp = new ParteIIBean();

                    parteII_temp.setIdParteII(rs.getInt("idparteII"));
                    parteII_temp.setNome(rs.getString("nome"));
                    parteII_temp.setPrenome(rs.getString("prenome"));
                    parteIIs.add(parteII_temp);

                }
                DBController.getInstance().closeAll(conn, null, null);
                return (ParteIIBean[]) parteIIs.toArray(new ParteIIBean[0]);


            } else {
                DBConnectionException dbException = new DBConnectionException(" Erro de conex�o com banco ");
                dbException.printStackTrace();
                throw dbException;
            }
        } catch (NullPointerException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}
